# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
import datetime
import time
import re
import csv
from pymongo import MongoClient
from utils.HttpUtils import HttpUtils


requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""
百度指数-搜索指数
area区域。全国0
"""


class Test_Get():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        # requests.utils.dict_from_cookiejar(html.cookies)  # 获取cookies转dict
        # res.cookies.get_dict()  # 获取cookies
        self.cookie = cookiejar.CookieJar()
        # ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        # ua = UserAgent(verify_ssl=False)  #
        self.headers = {
            'Accept': 'application/json, text/plain, */*',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Cache-Control': 'no-cache',
            'Cookie': 'BIDUPSID=E34F22598FD8C95828ED228992AC8037; PSTM=1611458199; BAIDUID=E34F22598FD8C95815CAB982510B59EE:FG=1; BAIDUID_BFESS=E34F22598FD8C95815CAB982510B59EE:FG=1; delPer=0; PSINO=2; H_PS_PSSID=33425_33272_31253_33338_26350_22158; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BA_HECTOR=05218ka5ag2g81a4hf1g0pqib0r; Hm_lvt_d101ea4d2a5c67dab98251f0b5de24dc=1611459149; BDUSS=mRiSmllTGVnbUh2N3huZEpQVEtmNUtraUpKMnVialBQbWJxVGtCZ3ljNnJkalJnRUFBQUFBJCQAAAAAAAAAAAEAAABI~H8xa29hbGFfc2VhAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKvpDGCr6Qxga; BDUSS_BFESS=mRiSmllTGVnbUh2N3huZEpQVEtmNUtraUpKMnVialBQbWJxVGtCZ3ljNnJkalJnRUFBQUFBJCQAAAAAAAAAAAEAAABI~H8xa29hbGFfc2VhAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKvpDGCr6Qxga; CHKFORREG=4cc2ce825a18119dd21a474e6167560c; bdindexid=tsihct3vmac894h4ok0g7ahm27; Hm_lpvt_d101ea4d2a5c67dab98251f0b5de24dc=1611459223; RT="z=1&dm=baidu.com&si=qdmb9uk8bin&ss=kkal64zi&sl=6&tt=ekf&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf"; ab_sr=1.0.0_YmE1ODlhNTRlOWJkNTE1Y2FlYjBkZWY3MmUxZDI2ZjAyNGFkNDk3YTU2OGM2MjZjNWIyYTY4Y2NiZGU3ZmVhZDc5YTRiZjI4NjQxNjY1MmQ5MGQ4MzQ3NmFlOTE0ZWIx; __yjsv5_shitong=1.0_7_736e406ea69d848f048d76f7a1c64ed7fa0f_300_1611459004589_116.140.63.172_95ff8d90',
            'DNT': '1',
            'Host': 'index.baidu.com',
            'Pragma': 'no-cache',
            'Proxy-Connection': 'keep-alive',
            'Referer': 'http://index.baidu.com/v2/main/index.html',
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36',
            'X-Requested-With': 'XMLHttpRequest',

        }
        self.itemDict = dict()


    def get_contents(self, keyword, start='2020-12-01', end='2021-01-23'):

        for date in self.dateRange(start, end):
            self.itemDict[date] = ""
        keyword = str(keyword).replace("'", '"')
        url = f'http://index.baidu.com/api/SearchApi/index?area=0&word={keyword}&area=0&startDate={start}&endDate={end}'

        resp = HttpUtils.do_request("GET", url, self.headers, "")
        if resp.status_code != 200:
            print('获取指数失败')

        content = resp.json()
        data = content.get('data')
        user_indexes = data.get('userIndexes')[0]
        uniqid = data.get('uniqid')
        ptbk = self.get_ptbk(uniqid)

        while ptbk is None or ptbk == '':
            ptbk = self.get_ptbk(uniqid)

        all_data = user_indexes.get('all').get('data')
        result = self.decrypt(ptbk, all_data)
        result = result.split(',')
        count = 0
        for k, v in self.itemDict.items():
            self.itemDict[k] = result[count]
            count += 1

        print(result)
        print(str(self.itemDict))

    def decrypt(self, t, e):
        n = list(t)
        i = list(e)
        a = {}
        result = []
        ln = int(len(n)/2)
        start = n[ln:]
        end = n[:ln]
        for j,k in zip(start, end):
            a.update({k: j})
        for j in e:
            result.append(a.get(j))
        return ''.join(result)

    def get_ptbk(self, uniqid):
        url = 'http://index.baidu.com/Interface/ptbk?uniqid={}'
        resp = HttpUtils.do_request("GET", url.format(uniqid), self.headers, "")
        if resp.status_code != 200:
            print('获取uniqid失败')
        return resp.json().get('data')

    # 获取时间范围内日期列表
    def dateRange(self, beginDate, endDate):
        datesList = []
        dt = datetime.datetime.strptime(beginDate, "%Y-%m-%d")
        date = beginDate[:]
        while date <= endDate:
            datesList.append(date)
            dt = dt + datetime.timedelta(1)
            date = dt.strftime("%Y-%m-%d")
        return datesList


if __name__ == '__main__':
    # conn = MongoClient('127.0.0.1', 27017)
    # db = conn["Test"]
    test_get = Test_Get()
    words = [[{"name": "雅诗兰黛", "wordType": 1}]]
    test_get.get_contents(words)
